package com.fare.taluo.core.domain.config.channel.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.dayatang.domain.DataPage;
import com.dayatang.domain.QuerySettings;
import com.dayatang.hibernate.EntityRepositoryHibernate;
import com.fare.taluo.core.domain.config.channel.SourceEntity;
import com.fare.taluo.core.domain.config.channel.SourceEntityRepository;

@Repository
public class SourceEntityRepositoryImpl extends EntityRepositoryHibernate
		implements SourceEntityRepository {

	@Override
	public List<SourceEntity> findSourceEntityListByConditions(String name,
			String url, String webTyle, String sorttype, int pageNum,
			int pageSize) {
		String hql = "from SourceEntity t";
		String condition = "";

		List<Object> list = new ArrayList<Object>();
		if (StringUtils.isNotEmpty(name)) {
			condition = condition + " where t.name like '%" + name + "%'";
		}
		if (StringUtils.isNotEmpty(url)) {
			/*
			 * if(StringUtils.isNotEmpty(condition)){ condition =
			 * condition + " and t.url = ?"; }else{ condition = condition +
			 * " where t.url = ?"; }
			 */
			// 变成模糊查询
			if (StringUtils.isNotEmpty(condition)) {
				condition = condition + " and t.url like '%" + url + "%'";
			} else {
				condition = condition + " where t.url like '%" + url + "%'";
			}
			// list.add(url);
		}

		if (StringUtils.isNotEmpty(webTyle)) {
			if (StringUtils.isNotEmpty(condition)) {
				condition = condition + " and t.webTyle = ?";
			} else {
				condition = condition + " where t.webTyle = ?";
			}
			list.add(webTyle);
		}

		hql = hql + condition;
		if (StringUtils.isNotBlank(sorttype)) {
			if (StringUtils.equalsIgnoreCase(sorttype, "n")) {
				hql += " order by t.name";
			}
			if (StringUtils.equalsIgnoreCase(sorttype, "u")) {
				hql += " order by t.url";
			}
			if (StringUtils.equalsIgnoreCase(sorttype, "t")) {
				hql += " order by t.webTyle";
			}
		}
		DataPage page = find(hql, list.toArray(), pageNum, pageSize,
				DataPage.class);
		List<SourceEntity> lists = page.getPageData();
		return lists;
	}

	@Override
	public SourceEntity updateSourceEntity(Long id, Integer crawlStatus,
			Long userId) {
		SourceEntity source = super.get(SourceEntity.class, id);
		source.setCrawlStatus(crawlStatus);
		source.setUserId(userId);
		source.setUserCreateTime(new Date());
		return super.save(source);
	}

	@Override
	public int getSourceEntityCount(String name, String url, String webTyle) {
		String hql = "from SourceEntity t";
		String condition = "";

		List<Object> list = new ArrayList<Object>();
		if (StringUtils.isNotEmpty(name)) {
			condition = condition + " where t.name like '%" + name + "%'";
		}
		if (StringUtils.isNotEmpty(url)) {
			/*
			 * if(StringUtils.isNotEmpty(condition)){ condition =
			 * condition + " and t.url = ?"; }else{ condition = condition +
			 * " where t.url = ?"; } list.add(url);
			 */
			// 变成模糊查询
			if (StringUtils.isNotEmpty(condition)) {
				condition = condition + " and t.url like '%" + url + "%'";
			} else {
				condition = condition + " where t.url like '%" + url + "%'";
			}
		}

		if (StringUtils.isNotEmpty(webTyle)) {
			if (StringUtils.isNotEmpty(condition)) {
				condition = condition + " and t.webTyle = ?";
			} else {
				condition = condition + " where t.webTyle = ?";
			}
			list.add(webTyle);
		}

		hql = hql + condition;
		List<SourceEntity> lists = super.find(hql, list.toArray(),
				SourceEntity.class);
		return lists.size();
	}

	@Override
	public List<SourceEntity> findAllSourceEntity() {
		return super.findAll(SourceEntity.class);
	}

	@Override
	public SourceEntity getSourceEntityByName(String name) {
		String hql = "from SourceEntity t";
		String condition = "";
		List<Object> list = new ArrayList<Object>();
		if (StringUtils.isNotEmpty(name)) {
			condition = condition + " where t.name like '%" + name + "%'";
		}
		hql = hql + condition;
		List<SourceEntity> lists = super.find(hql, list.toArray(),
				SourceEntity.class);
		if (lists != null && lists.size() > 0) {
			return lists.get(0);
		} else {
			return null;
		}
	}

	@Override
	public List<SourceEntity> findSourceEntityListByChanelName(String name) {
		// TODO Auto-generated method stub
		String hql = "from SourceEntity t";
		String condition = "";

		List<Object> list = new ArrayList<Object>();
		if (StringUtils.isNotBlank(name)) {
			condition = condition + " where t.name like '%" + name + "%'";
		}
		hql += condition;
		List<Object> listcondition = new ArrayList<Object>();
		List<SourceEntity> lists = super.find(hql, listcondition.toArray(),
				SourceEntity.class);
		return lists;
	}

	@Override
	public List<SourceEntity> findSourceEntityListByWbid(String webTyle) {
		String hql = " from SourceEntity t where 1=1";
		List<Object> list = new ArrayList<Object>();
		if (StringUtils.isNotEmpty(webTyle)) {
			hql += " and t.webTyle = ?";
			list.add(webTyle);
		}
		return super.find(hql, list.toArray(), SourceEntity.class);
	}

	@Override
	public List<SourceEntity> findSourceEntityListByCrawlStatus(int crawlStatus) {
		// TODO Auto-generated method stub
		String hql = " from SourceEntity t where 1=1";
		List<Object> list = new ArrayList<Object>();
		hql += " and t.crawlStatus = ?";
		list.add(crawlStatus);
		return super.find(hql, list.toArray(), SourceEntity.class);
	}

	@Override
	public List<SourceEntity> findSourceEntityListByid(Long id) {
		String hql = "from SourceEntity t where t.id = ?";
		// TODO Auto-generated method stub
		List<Object> list = new ArrayList<Object>();
		list.add(id);
		return super.find(hql, list.toArray(), SourceEntity.class);
	}

	@Override
	public List<SourceEntity> findSourceEntityByCk(String ck) {
		// TODO Auto-generated method stub
		String hql = "from SourceEntity s where s.carrierKey = ?";
		List<Object> list = new ArrayList<Object>();
		list.add(ck);
		return this.find(hql, list.toArray(), SourceEntity.class);
	}

	/**
	 * 找出存在该航线的航空公司
	 * 
	 * @param fromCity
	 * @param toCity
	 * @return
	 * @author zengxiangqi
	 */
	@Override
	public List<SourceEntity> findSourceEntityListByLine(String fromCity,
			String toCity) {
		// select s.carrier_key from fdc_flightinfo f ,fdc_source s where f.company_code = s.carrier_key and
		// f.fromcity='PEK' and f.tocity='CAN' group by f.fromcity,f.tocity,
		// s.carrier_key
		if(StringUtils.isNotEmpty(fromCity)&&StringUtils.isNotEmpty(toCity)){
			String hql = "select SourceEntity from SourceEntity s,FlightInfoEntity f" +
					        " where f.companyCode = s.carrierKey" +
							" and f.fromCityName = ?" +
							" and f.toCityName = ?" +
							" group by f.fromCityName,f.toCityName,s.carrierKey";
			
			List<Object> list = new ArrayList<Object>();
			list.add(fromCity);
			list.add(toCity);
			try {
				return this.find(hql, list.toArray(),null);
			} catch (Exception e) {
				e.printStackTrace();
			}
		
			
		}else{
			return null;
		}
		return null;
	

	}
	
	public List<SourceEntity> findSourceEntityListByCrawlLine(String fromCity,
			String toCity){
		if(StringUtils.isNotEmpty(fromCity)&&StringUtils.isNotEmpty(toCity)){
			String hql = "from SourceEntity s,TaskTemplateEntity t" +
					        " where s.webTyle = 1 and t.attachHbaseKey = s.hbaseKey" +
							" and t.fromCity = ?" +
							" and t.toCity = ?" +
							" group by t.fromCity,t.toCity,s.carrierKey";
			
			List<Object> list = new ArrayList<Object>();
			list.add(fromCity);
			list.add(toCity);
			List<SourceEntity> listReturn = null;
			try {
				listReturn = new ArrayList<SourceEntity>();
				Query query = getSession().createQuery(hql);
				for (int i = 0; i < list.toArray().length; i++) {
					query = query.setParameter(i, list.get(i));
				}
				List<Object[]> results = query.list();
				if(results != null && !results.isEmpty()){
					for(Object[] o : results){
						//spe = (SourceEntity)o[0];
						//spe.setPlaneInfoJsonEntity((SourceEntity)o[1]);
						listReturn.add((SourceEntity)o[0]);
					}
				}
				return listReturn;
			} catch (Exception e) {
				e.printStackTrace();
			}
		
			
		}else{
			return null;
		}
		return null;
	}
	
	@Override
	public List<SourceEntity> findSourceEntityListByCarrierKey(String carrierKey){
		if(StringUtils.isNotEmpty(carrierKey)){
			String hql = "from SourceEntity s" +
					     " where s.carrierKey=? " ;		
			List<Object> list = new ArrayList<Object>();
			list.add(carrierKey);
			
				return super.find(hql, list.toArray(),null);			
		}else{
			return null;
		}	
	}
	
	public SourceEntity findByHbaseKey(String hbaseKey){
		return this.getSingleResult(QuerySettings.create(SourceEntity.class).eq("hbaseKey", hbaseKey));		
	}

	@Override
	public List<SourceEntity> findCrawlAirCompanys() {
		
		StringBuffer bf=new StringBuffer();
		List<Object> params=new ArrayList<Object>();
		bf.append("select s from SourceEntity s where 1=1 ");
		bf.append("and s.webTyle=? ");
		params.add("1");
		bf.append("and s.crawlStatus=?");
		params.add(new Integer(1));
		return super.find(bf.toString(), params.toArray(), SourceEntity.class);
		
	}

	@Override
	public List<SourceEntity> findCrawlChannel() throws Exception {
		StringBuffer bf=new StringBuffer();
		List<Object> params=new ArrayList<Object>();
		bf.append("select s from SourceEntity s where 1=1 ");
		bf.append("and s.webTyle=? ");
		params.add("0");
		bf.append("and s.crawlStatus=? order by s.id desc");
		params.add(new Integer(1));
		return super.find(bf.toString(), params.toArray(), SourceEntity.class);
	}

	@Override
	public List<SourceEntity> findByCarrierKey(String carrierKey) {
		if(StringUtils.isNotEmpty(carrierKey)){
			String hql = "from SourceEntity s where s.carrierKey=? " ;		
			List<Object> list = new ArrayList<Object>();
			list.add(carrierKey);
			
				return super.find(hql, list.toArray(),SourceEntity.class);			
		}else{
			return null;
		}	
		
	}
	

	@Override
	public SourceEntity find(Long id) throws Exception {
		return super.get(SourceEntity.class, id);
	}
}
